<?php

namespace app\model\order;

use app\model\user\User;
use think\Model;

/**
 * t_order_refund 订单退款表
 * @property integer $id (主键)
 * @property integer $order_id 订单表ID
 * @property integer $order_type 订单类型 1:普通订单 2:拼团订单 3:秒杀订单 4:砍价订单
 * @property string $order_no 订单号
 * @property string $refund_order_no 退款订单号
 * @property integer $user_id 用户UID
 * @property string $user_name 用户名
 * @property integer $step 步骤号
 * @property integer $refund_type 退款申请类型 1:仅退款 2:退货退款
 * @property integer $refund_way 退款方式 1:整单退 2:退部分
 * @property integer $refund_num 退款件数
 * @property string $refund_price 申请退款金额
 * @property string $refunded_price 已退款金额
 * @property string $refund_phone 退款电话
 * @property string $refund_express 退货快递单号
 * @property string $refund_express_name 退货快递名称
 * @property string $apply_refund_reason 申请退款原因
 * @property string $refund_img 退款图片
 * @property string $unrefund_reason 不同意退款的原因
 * @property string $remark 备注
 * @property string $refunded_time 处理时间
 * @property string $apply_refund_data 退款商品信息
 * @property integer $status 状态 1:待审批 2:通过 3:拒绝 4:取消
 * @property string $third_return_msg 第三方支付返回信息
 * @property string $create_time 创建时间
 * @property string $update_time 更新时间
 */
class OrderRefund extends Model
{
    /**
     * The connection name for the model.
     *
     * @var string|null
     */
    protected $connection = 'mysql';

    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 't_order_refund';

    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $pk = 'id';

    //支付状态 状态 1:待审批 2:通过 3:拒绝 4:取消
    const STATUS_WAIT = 1;  //待审批
    const STATUS_PASS = 2;  //已通过
    const STATUS_REFUSE = 3;    //已拒绝
    const STATUS_CANCEL = 4;  //已取消

    const STATUS_TEXT_MAP = [
        self::STATUS_WAIT => "待审批",
        self::STATUS_PASS => "已通过",
        self::STATUS_REFUSE => "已拒绝",
        self::STATUS_CANCEL => "已取消",
    ];
    protected $append = [
        'status_text'
    ];

    public function user()
    {
        return $this->belongsTo(User::class, "user_id", "id");
    }

    public function getStatusTextAttr($value, $data)
    {
        return self::STATUS_TEXT_MAP[$data['status']] ?? "";
    }

    public function getRefundImgAttr($value)
    {
        return json_decode($value ?? "[]", true);
    }

}
